package com.doeis.minaserver;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;


public class SystemProperties {
	
	
	//服务器生成群号时一次取一批群号所使用的步长
	public static final String SERVER_GROUP_STEPLEN = "server.group.stepLen";
	
	//主机地址、端口
	public static final String PRO_SERVER_HOST = "server.host";
	public static final String PRO_SERVER_PORT = "server.port";
	
	//是否打印日志 
	public static final String PRO_LOG_ERROR = "server.log.error";
	
	//服务器io处理线程数及并发线程数
	public static final String PRO_SERVER_PROCESSOR_NUM = "server.processor.num";
	public static final String PRO_SERVER_HANDLE_MIN = "server.handle.minsize";
	public static final String PRO_SERVER_HANDLE_MAX = "server.handle.maxsize";
	
	public static final String PRO_WEB_DOMAIN = "web.domain";
	public static final String PRO_WEB_CONTEXT_PATH = "web.context.path";
	
	//异步写入数据库
	public static final String PRO_DB_MSG_NUM = "db.msg.num";
	public static final String PRO_DB_MSG_TIME = "db.msg.time";
	public static final String PRO_DB_OFFLINE_NUM = "db.offline.num";
	public static final String PRO_DB_OFFLINE_TIME = "db.offline.time";
	
	//邮件配置
	public static final String PRO_MAIL_SMTP_HOST = "mail.smtp.host";
	public static final String PRO_MAIL_SMTP_PORT = "mail.smtp.port";
	public static final String PRO_MAIL_SMTP_AUTH = "mail.smtp.auth";
	public static final String PRO_MAIL_SMTP_AUTH_USER = "mail.smtp.auth.user";
	public static final String PRO_MAIL_SMTP_AUTH_PW = "mail.smtp.auth.password";
	public static final String PRO_MAIL_FROM_ADDR = "mail.from.address";
	public static final String PRO_MAIL_TO_CUSTOMER_SERVICE = "mail.to.service";
	
	
	//极光推送 程序ID 设置developer平台的ApiKey/SecretKey
	public static final String PRO_PUSH_JPUSH_APIKEY = "push.jpush.apikey";
	public static final String PRO_PUSH_JPUSH_SECRETKEY = "push.jpush.secretkey";
	public static final String PRO_PUSH_JPUSH_APIKEY_APPSTORE = "push.jpush.apikey.appstore";
	public static final String PRO_PUSH_JPUSH_SECRETKEY_APPSTORE= "push.jpush.secretkey.appstore";
	
	public static final String PRO_PUSH_JPUSH_APIKEY_ANDROID = "push.jpush.apikey.android";
	public static final String PRO_PUSH_JPUSH_SECRETKEY_ANDROID = "push.jpush.secretkey.android";
	
	public static final String PRO_PUSH_JPUSH_DEPLOY_STATUS = "push.jpush.deploy.status";
	
	//消息队列的配置
	public static final String PRO_MQ_BROKERNAME = "mq.brokername";
	public static final String PRO_MQ_URI = "mq.uri";
	public static final String PRO_MQ_KAHADB = "mq.kahadb";
	public static final String PRO_MQ_USERJMX = "mq.usejmx";
	
	
	
	private static Properties properties = null;
	private static final String propertyFileName = "system.properties";
	
	private static int groupStepLen;
	
	private static String host;
	private static int port;
	private static boolean logError;
	
	private static int processorNum;
	private static int minHandle;
	private static int maxHandle;
	
	private static String webDomain;
	private static String webContextPath;
	
	private static int msgNum;
	private static long msgTime;
	private static int offlineNum;
	private static long offlineTime;
	
	private static String smtpHost;
	private static String smtpPort;
	private static boolean smtpAuth;
	private static String smtpUser;
	private static String smtpPassword;
	private static String mailFrom;
	private static String serviceMail;
	
	private static Integer groupAmountLimit;
	private static Integer groupMemberAmountLimit;
	
	//消息队列的配置
	private static String mqBrokerName;
	private static String mqURI;
	private static String mqKahaDB;
	private static boolean mqUserJmx;
	
	private static String pushApiKey = "";
	private static String pushSecretKey = "";
	private static String pushApiKeyAppstore = "";
	private static String pushSecretKeyAppstore = "";
	private static Integer pushDeployStatus = 1;
	
	
	private static String pushApiKeyAndroid = "";
	private static String pushSecretKeyAndroid= "";
	
	static{
		loadPropery();
	}
	
	public static int getGroupStepLen() {
		return groupStepLen;
	}

	public static String getSystemHost(){
		return host;
	}
	
	public static String getPushApiKeyAndroid() {
		return pushApiKeyAndroid;
	}

	public static void setPushApiKeyAndroid(String pushApiKeyAndroid) {
		SystemProperties.pushApiKeyAndroid = pushApiKeyAndroid;
	}

	public static String getPushSecretKeyAndroid() {
		return pushSecretKeyAndroid;
	}

	public static void setPushSecretKeyAndroid(String pushSecretKeyAndroid) {
		SystemProperties.pushSecretKeyAndroid = pushSecretKeyAndroid;
	}

	public static int getSystemPort(){
		return port;
	}
	
	public static int getProcessorNum() {
		return processorNum;
	}

	public static int getMinHandle() {
		return minHandle;
	}

	public static int getMaxHandle() {
		return maxHandle;
	}

	public static String getWebDomain(){
		return webDomain;
	}
	
	public static String getWebContextPath(){
		return webContextPath;
	}
	
	public static int getMsgNum(){
		return msgNum;
	}
	
	public static long getMsgTime(){
		return msgTime;
	}
	
	public static int getOfflineNum() {
		return offlineNum;
	}

	public static long getOfflineTime() {
		return offlineTime;
	}

	public static String getSmtpHost() {
		return smtpHost;
	}

	public static String getSmtpPort() {
		return smtpPort;
	}

	public static boolean isSmtpAuth() {
		return smtpAuth;
	}

	public static String getSmtpUser() {
		return smtpUser;
	}

	public static String getSmtpPassword() {
		return smtpPassword;
	}
	
	public static String getMailFrom(){
		return mailFrom;
	}
	
	
	
	public static String getServiceMail() {
		return serviceMail;
	}

	public static Integer getGroupAmountLimit() {
		return groupAmountLimit;
	}

	public static Integer getGroupMemberAmountLimit() {
		return groupMemberAmountLimit;
	}
	
	public static String getMqBrokerName() {
		return mqBrokerName;
	}
	
	public static boolean isLogError() {
		return logError;
	}

	public static String getMqURI() {
		return mqURI;
	}

	public static String getMqKahaDB() {
		return mqKahaDB;
	}

	public static boolean isMqUserJmx() {
		return mqUserJmx;
	}
	
	public static String getPushApiKey() {
		return pushApiKey;
	}

	public static String getPushSecretKey() {
		return pushSecretKey;
	}
	
	public static String getPushApiKeyAppstore() {
		return pushApiKeyAppstore;
	}

	public static String getPushSecretKeyAppstore() {
		return pushSecretKeyAppstore;
	}

	public static Integer getPushDeployStatus() {
		return pushDeployStatus;
	}

	private static void loadPropery(){
		InputStream in = null;
		try{
			in = SystemProperties.class.getClassLoader().getResourceAsStream(propertyFileName); 
			if (in != null) {
				properties = new Properties();
				properties.load(in);

				groupStepLen = Integer.valueOf(properties.getProperty(SERVER_GROUP_STEPLEN, "100"));
				
				host = properties.getProperty(PRO_SERVER_HOST, "0.0.0.0");
				port = Integer.valueOf( properties.getProperty(PRO_SERVER_PORT, "80") );	
				
				logError =  Boolean.valueOf(properties.getProperty(PRO_LOG_ERROR, "false"));
				
				processorNum = Integer.valueOf( properties.getProperty(
						PRO_SERVER_PROCESSOR_NUM, 
						Runtime.getRuntime().availableProcessors()+1+"") );
				minHandle = Integer.valueOf( properties.getProperty(PRO_SERVER_HANDLE_MIN, "0") );
				maxHandle = Integer.valueOf( properties.getProperty(PRO_SERVER_HANDLE_MAX, "10") );
				
				webDomain = properties.getProperty(PRO_WEB_DOMAIN,"127.0.0.0");
				webContextPath = properties.getProperty(PRO_WEB_CONTEXT_PATH, "/");
				
				msgNum = Integer.valueOf( properties.getProperty(PRO_DB_MSG_NUM, "500") );
				msgTime = Integer.valueOf( properties.getProperty(PRO_DB_MSG_TIME, "10") ) * 1000;
				offlineNum = Integer.valueOf( properties.getProperty(PRO_DB_OFFLINE_NUM, "500") );
				offlineTime = Integer.valueOf( properties.getProperty(PRO_DB_OFFLINE_TIME, "5") ) * 1000;
				
				smtpHost = properties.getProperty(PRO_MAIL_SMTP_HOST,"mail.16ds.com");
				smtpPort = properties.getProperty(PRO_MAIL_SMTP_PORT,"25");
				smtpAuth = "true".equalsIgnoreCase(properties.getProperty(PRO_MAIL_SMTP_AUTH));
				smtpUser = properties.getProperty(PRO_MAIL_SMTP_AUTH_USER);
				smtpPassword = properties.getProperty(PRO_MAIL_SMTP_AUTH_PW);
				mailFrom = properties.getProperty(PRO_MAIL_FROM_ADDR);
				serviceMail = properties.getProperty(PRO_MAIL_TO_CUSTOMER_SERVICE, "customer_service@16ds.com");
				
				mqBrokerName = properties.getProperty(PRO_MQ_BROKERNAME, "16dsBroker");
				mqURI = properties.getProperty(PRO_MQ_URI, "tcp://0.0.0.0:61616");
				mqUserJmx = Boolean.valueOf(properties.getProperty(PRO_MQ_USERJMX, "false"));
				mqKahaDB = properties.getProperty(PRO_MQ_KAHADB, "../data");
				
				pushApiKey =  properties.getProperty(PRO_PUSH_JPUSH_APIKEY, "");
				pushSecretKey =  properties.getProperty(PRO_PUSH_JPUSH_SECRETKEY, "");
				pushApiKeyAppstore =  properties.getProperty(PRO_PUSH_JPUSH_APIKEY_APPSTORE, "");
				pushSecretKeyAppstore =  properties.getProperty(PRO_PUSH_JPUSH_SECRETKEY_APPSTORE, "");
				pushDeployStatus = Integer.valueOf( properties.getProperty(PRO_PUSH_JPUSH_DEPLOY_STATUS, "1"));
				
				pushApiKeyAndroid =  properties.getProperty(PRO_PUSH_JPUSH_APIKEY_ANDROID, "");
				pushSecretKeyAndroid =  properties.getProperty(PRO_PUSH_JPUSH_SECRETKEY_ANDROID, "");
				
				
			} else {
				throw new RuntimeException ("Could not open an input stream on " + propertyFileName +".  Check that it is in the path " + System.getProperty("java.class.path"));
			}
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(in != null){
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public static void reloadPropery(){
		loadPropery();
	}

}
